<template>
  <div style="padding: 10px;">
    <Row :space="15">
      <Cell label="输出内容">
        <div class="h-input-group" v-for="(item, i) in formData.messages" :key="i" style="margin-bottom: 10px;">
          <Input type="text" v-model="item.varName" placeholder="变量名" />
          <span class="h-input-addon">=</span>
          <AutoComplete v-model="item.varValue" :mustMatch="false" type="title" :datas="parameters" placeholder="变量值" />
          <span class="h-input-addon link" @click="delMessageLine(i)">删除</span>
        </div>
        <span class="link" @click="addMessageLine">添加一行</span>
      </Cell>
      <Cell>
        <Button color="primary" @click="bindForm">设置</Button>
        <Space :width="15" />
        <Button @click="setForm" color="yellow">重置</Button>
      </Cell>
    </Row>
  </div>
</template>

<script>
import LogicFlow from '@logicflow/core';
import { message } from 'heyui';

export default {
  props: {
    lf: Object,
    currentNode: Object,
  },
  mounted() {
    this.setForm();
    this.getParameters();
  },
  data() {
    return {
      parameters: [],
      formData: {
        messages: []
      }
    }
  },
  methods: {
    setForm() {
      let id = this.$props.currentNode.data.id;
      let properties = this.$props.lf.getProperties(id);
      if (!!properties.messages) {
        this.formData.messages = properties.messages;
      }
    },
    bindForm() {
      let id = this.$props.currentNode.data.id;
      this.$props.lf.setProperties(id, this.formData);
      this.$emit('close', false);
      message.success("设置成功");
    },
    addMessageLine() {
      this.formData.messages.push({ varName: "", varValue: "", })
    },
    delMessageLine(index) {
      this.formData.messages.splice(index, 1);
    },
    getParameters() {
      let flowData = (this.$props.lf).getGraphRawData();
      let nodes = flowData["nodes"];
      let node = this.getStartNode(flowData);
      while (!!node && node?.id != this.$props.currentNode?.data?.id && node?.type != 'EndNode') {
        var properties = node?.properties;
        var name = properties?.name;
        var parameterName = properties?.parameterName;

        if (!!parameterName) {
          if (properties?.inputHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".input", value: "$" + parameterName + ".input" });
          } if (properties?.resultHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".output", value: "$" + parameterName + ".output" });
          }
        }
        node = this.getNextNode(flowData, node.id);
      }
    },
    getStartNode(flowData) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.type == 'StartNode') {
          return node;
        }
      }
    },
    getNode(flowData, id) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.id == id) {
          return node;
        }
      }
    },
    getNextNode(flowData, id) {
      let edges = flowData["edges"]
      for (var i = 0; i < edges.length; i++) {
        let edge = edges[i];
        if (edge.sourceNodeId == id) {
          return this.getNode(flowData, edge.targetNodeId);
        }
      }
    },
  }
}
</script>